package com.gt.web.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gt.app.domain.EsPatientInfo;
import com.gt.app.service.EsPatientInfoService;
import com.gt.common.core.domain.AjaxResult;
import com.gt.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 病人个人信息表 前端控制器
 * </p>
 *
 * @author caidesheng
 */
@Slf4j
@RestController
@RequestMapping("esPatientInfo")
@Api(tags = "病人个人信息接口")
public class EsPatientInfoController {

    @Autowired
    private EsPatientInfoService esPatientInfoService;

    @ApiOperation(value = "新增病人个人信息")
    @PostMapping(value = "add")
    public AjaxResult add(@RequestBody EsPatientInfo esPatientInfo) {

        try {
            if (esPatientInfo == null){
                return null;
            }
            LambdaQueryWrapper<EsPatientInfo> eq = new LambdaQueryWrapper<EsPatientInfo>().eq(EsPatientInfo::getEsCheckId, esPatientInfo.getEsCheckId());
            EsPatientInfo patientInfo = esPatientInfoService.getOne(eq);
            if (patientInfo != null){
                esPatientInfoService.remove(eq);
            }
            esPatientInfo.setUserId(SecurityUtils.getUserId());
            esPatientInfoService.save(esPatientInfo);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "根据checkId获取病人个人信息")
    @GetMapping(value = "getPatientInfo")
    public AjaxResult getPatientInfo(String checkId) {
        LambdaQueryWrapper<EsPatientInfo> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<EsPatientInfo> eq = queryWrapper.eq(EsPatientInfo::getEsCheckId, checkId);
        return AjaxResult.success(esPatientInfoService.getOne(eq));
    }


    //    @ApiOperation(value = "更新病人个人信息")
    @PutMapping(value = "update")
    public AjaxResult update(@RequestBody EsPatientInfo esPatientInfo) {
        try {
            esPatientInfoService.updateById(esPatientInfo);
            return AjaxResult.success(esPatientInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    //    @ApiOperation(value = "删除病人个人信息")
    @DeleteMapping(value = "delete")
    public AjaxResult delete(String id) {
        try {
            return AjaxResult.success(esPatientInfoService.removeById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

}
